package replication

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestTransactionPayloadEventDecode(t *testing.T) {
	e := &TransactionPayloadEvent{
		format: FormatDescriptionEvent{
			Version:                0x4,
			ServerVersion:          "8.0.27",
			CreateTimestamp:        0x0,
			EventHeaderLength:      0x13,
			EventTypeHeaderLengths: []uint8{0x38, 0xd, 0x0, 0x8, 0x0, 0x12, 0x0, 0x4, 0x4, 0x4, 0x4, 0x12, 0x0, 0x0, 0x5c, 0x0, 0x4, 0x1a, 0x8, 0x0, 0x0, 0x0, 0x8, 0x8, 0x8, 0x2, 0x0, 0x0, 0x0, 0xa, 0xa, 0xa, 0x19, 0x19, 0x0, 0x12, 0x34, 0x0, 0xa, 0x28, 0x0},
			ChecksumAlgorithm:      0x1,
		},
		Size:             91132,
		UncompressedSize: 625404,
		CompressionType:  ZSTD,
		Payload: []byte{
			0x28, 0xb5, 0x2f, 0xfd, 0x00, 0x58, 0xbc, 0x0a,
			0x00, 0xf6, 0x13, 0x44, 0x35, 0x60, 0x45, 0xd3,
			0x1c, 0x00, 0x92, 0x80, 0xa2, 0x43, 0x5d, 0x92,
			0xe0, 0x70, 0x43, 0xc5, 0x30, 0x0c, 0x12, 0x9c,
			0xdf, 0xcd, 0xfa, 0x0d, 0x1a, 0x06, 0x11, 0x11,
			0x91, 0x05, 0x79, 0x83, 0x3e, 0x4a, 0x41, 0xda,
			0xbb, 0xb5, 0x6e, 0xd4, 0xb7, 0xb0, 0x36, 0x45,
			0x98, 0x23, 0x74, 0x0d, 0x6b, 0x3c, 0xfa, 0xde,
			0x24, 0x05, 0x37, 0x00, 0x34, 0x00, 0x36, 0x00,
			0xec, 0x97, 0x4b, 0xfe, 0x33, 0x04, 0x9c, 0x27,
			0xeb, 0x41, 0xe2, 0x10, 0x69, 0x78, 0x45, 0xd9,
			0x6a, 0x52, 0x41, 0x91, 0xa0, 0x28, 0x6b, 0xa9,
			0x50, 0x50, 0xe8, 0xaf, 0x83, 0x5f, 0x87, 0x1c,
			0xc2, 0xa8, 0x15, 0xd5, 0x34, 0xfe, 0x3f, 0x72,
			0xf1, 0x07, 0xbb, 0xc2, 0xef, 0x78, 0xc1, 0x07,
			0x0e, 0xf1, 0x9f, 0x35, 0x9c, 0x27, 0x2b, 0x52,
			0x8c, 0xf4, 0x49, 0x67, 0xfb, 0x3e, 0x7a, 0x2d,
			0xec, 0x5a, 0xa5, 0x8d, 0xd6, 0x81, 0x53, 0xfe,
			0xcd, 0xe2, 0x7f, 0xfb, 0xd5, 0xbc, 0x35, 0x00,
			0xff, 0xd9, 0x02, 0xce, 0x93, 0xf5, 0x2b, 0xd3,
			0xb4, 0x33, 0xd6, 0x38, 0xa3, 0x19, 0x64, 0x9a,
			0xe6, 0x5d, 0x75, 0x9d, 0x58, 0x6c, 0xe9, 0x09,
			0x02, 0xc7, 0x03, 0xf3, 0x3a, 0xcf, 0x85, 0x11,
			0x52, 0x2a, 0x25, 0x7c, 0xd1, 0x75, 0x8d, 0xae,
			0x75, 0x9c, 0x87, 0xc6, 0x87, 0xfd, 0x0a, 0x98,
			0x0c, 0x38, 0x4f, 0x18, 0x8c, 0x35, 0x65, 0x6c,
			0x9b, 0xde, 0xe7, 0x1e, 0x28, 0x59, 0xe9, 0x38,
			0x77, 0xba, 0x48, 0x8a, 0xb1, 0x3e, 0x91, 0x8d,
			0xe4, 0x8c, 0xee, 0xeb, 0x5a, 0x6f, 0xf7, 0x75,
			0x01, 0xff, 0xff, 0x3f, 0x22, 0xe1, 0x3f, 0x73,
			0xe0, 0x6c, 0x61, 0x87, 0x0f, 0x9c, 0xa7, 0xeb,
			0xb2, 0xaa, 0xad, 0xba, 0x56, 0x2e, 0xff, 0x05,
			0x02, 0x4c, 0x19, 0x53, 0xe8, 0x76, 0xcf, 0xea,
			0xae, 0xe4, 0x40, 0x55, 0x82, 0x8f, 0x46, 0x11,
			0x84, 0xde, 0x9c, 0x33, 0x42, 0xea, 0x60, 0x43,
			0xd2, 0x94, 0x8f, 0x0c, 0x18, 0x00, 0x56, 0x15,
			0x08, 0x52, 0xd0, 0x00, 0x10, 0x06, 0xac, 0x2e,
			0xd7, 0x32, 0x8a, 0xa4, 0x84, 0x0c, 0x48, 0x42,
			0x80, 0x44, 0x9a, 0xe5, 0x02, 0x37, 0x9b, 0x87,
			0x24, 0x02, 0x01, 0x3d, 0x86, 0x50, 0x4d, 0x3e,
			0x91, 0x39, 0x40, 0x0d, 0xb0, 0xaf, 0xf0, 0x09,
			0xfa, 0x9a, 0x1e, 0x43, 0xd4, 0x60, 0x82, 0x84,
			0x37, 0x81, 0x78, 0x55, 0xcd, 0x00, 0x0c, 0x27,
			0x78, 0x10, 0x08, 0x0d, 0x06, 0x19, 0x80, 0x17,
			0x01, 0x00, 0x00,
		},
	}
	err := e.decodePayload()
	require.NoError(t, err)

	// Check raw events
	require.Len(t, e.Events, 8)
	require.Equal(t, QUERY_EVENT, e.Events[0].Header.EventType)
	require.Equal(t, TABLE_MAP_EVENT, e.Events[1].Header.EventType)
	require.Equal(t, WRITE_ROWS_EVENTv2, e.Events[2].Header.EventType)
	require.Equal(t, TABLE_MAP_EVENT, e.Events[3].Header.EventType)
	require.Equal(t, UPDATE_ROWS_EVENTv2, e.Events[4].Header.EventType)
	require.Equal(t, TABLE_MAP_EVENT, e.Events[5].Header.EventType)
	require.Equal(t, DELETE_ROWS_EVENTv2, e.Events[6].Header.EventType)
	require.Equal(t, XID_EVENT, e.Events[7].Header.EventType)

	// Check insert/update/delete rows events casting
	ievent, ok := e.Events[2].Event.(*RowsEvent)
	require.True(t, ok)
	require.Equal(t, ievent.Type(), EnumRowsEventTypeInsert)

	uevent, ok := e.Events[4].Event.(*RowsEvent)
	require.True(t, ok)
	require.Equal(t, uevent.Type(), EnumRowsEventTypeUpdate)

	devent, ok := e.Events[6].Event.(*RowsEvent)
	require.True(t, ok)
	require.Equal(t, devent.Type(), EnumRowsEventTypeDelete)
}
